package controllers;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.ButtonRequest;
import model.ButtonRequestModel;
import model.TaarifModel;
import view.EditTaarifGUI;
import view.TaarifChangeGUI;
import client.CliMessage;
/**
 * This Class is a Customer Taarif Controller which implements Observer Object.
 * @author Tal & Yuda
 *
 */
// 

public class EditTaarifController extends AbstractController
{	
				private EditTaarifGUI EditTaarifView;
				private ButtonRequestModel ButtonRequestModel;
	
				
				 /**
				 * This is edit tarif controller constructor.
				 * @param taarifView is a taarif window GUI instance.
				 * @param ButtonRequestModel is a ButtonRequest Model instance.
				 */
				public EditTaarifController(EditTaarifGUI taarifView, ButtonRequestModel ButtonRequestModel) 
				{
					super(taarifView,ButtonRequestModel);
					this.EditTaarifView = taarifView;
					this.ButtonRequestModel = ButtonRequestModel;
				
				}
				
				/**
				 * This is setTaarifButtons method-this method add listeners to buttons in Taarif window.
				 * 
				 */
				
				private void setTaarifButtons(ArrayList<ButtonRequest>  btnReq)
				{
					
					int nButtons = btnReq.size();
					
					
					//set a HashMap that contains all possible button listeners by their names
					ArrayList<ButtonListener> al = new ArrayList<ButtonListener>();
					ArrayList<ActionListener>  btnLis=new ArrayList<ActionListener>();
					
					//set  specific complaints buttons
					for(int i = 0; i < nButtons ; i++)
					{
						al.add(new ButtonListener(this,btnReq.get(i).getButtonReqID()));
						btnLis.add(al.get(i));
						EditTaarifView.addButtonListener(btnLis.get(i),i);
					}	
				
				}
				
				
				
				/**
				 * Inner class that handles when Button Pressed by request Id, implements Action listener
				 * @author Tal & Yuda
				 *
				 */
				
				class ButtonListener extends AbstractListener
				{
					private EditTaarifController previusObserver;
					private int serviceID;
					
					public ButtonListener(EditTaarifController previusObserver, String serviceID)
					{
						this.serviceID = Integer.parseInt(serviceID);
						this.previusObserver=previusObserver;
					}
					
					@Override
					public void actionPerformed(ActionEvent arg0) 
					{
						

						//create a new Complaint Details controller
						TaarifChangeGUI  taarifView = new TaarifChangeGUI();
						TaarifModel taarifModel = new TaarifModel(serviceID);
						
						TaarifChangeController tarController = new TaarifChangeController(taarifView, taarifModel);
						
						
						//add observer to new controller and delete the rest
						setCurrentObserver(tarController);						
						
						//add a listener to "back" button in Unhandled Complaints Details window
						setBackListener(previusObserver,taarifView);
						
							
						//remove buttons from arrayList
						ButtonRequestModel.RemoveButtonRequest();
						
						String searchQuery = "search"	+ "SELECT tar.description,tar.serviceid,tar.price "
								+" FROM taarif AS tar"
						        +" WHERE tar.serviceid = " +  serviceID + "";
                               

                          sendToServer(searchQuery);						                       											
					}
				}



				@Override
				public void handleDBRequestSuccess()
				{
					EditTaarifView.displayWrnMessage("Edit Taarif Request has been sent to CEO.");
					
				}
				
				@Override
				public void handleDBSearchResult(CliMessage dbResult) 
				{	
					//get a list of all taarif
					ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
					ArrayList<ButtonRequest>  btnReq= ButtonRequestModel.getButtonRequest();
					int rows=srs.size();
					int column=3;
					
					//create data table
					 Object[][] data=new Object[rows][column];
				   	
					 String[] columnNames={"Service ID", "Description", "Price"};

					
					for(int i = 0 ; i <rows  ; i++)
					{
						Map<String, String> taarifsList  = srs.get(i);	
						
	                    //add serviceID button to arraylist
						btnReq.add(new ButtonRequest(taarifsList.get("SERVICEID"),(taarifsList.get("SERVICEID"))));
								
						// service id
						 data[i][0]=taarifsList.get("SERVICEID");
						// description
						 data[i][1]=taarifsList.get("DESCRIPTION");
						// price
						 data[i][2]=taarifsList.get("PRICE");
						
						 System.out.println("Item number: "+taarifsList.get("SERVICEID")+" "+"description: :"+taarifsList.get("DESCRIPTION")+" "+"price: "+taarifsList.get("PRICE"));
							 					
					}	
					
					//set new data table
					EditTaarifView.setTable(columnNames, data);
					
					//create n buttons
					int numberOfRequests = btnReq.size();
					
					EditTaarifView.setChangeTaarifbuttons(numberOfRequests);
					
					//Set list button  listener	
					setTaarifButtons(btnReq);
					 
					
					//display main window
					EditTaarifView.setVisible(true);
					EditTaarifView.setTableVisibilty(true);	
							
				}

				@Override
				public void handleNullSearchResult() 
				{
					//display main window
					EditTaarifView.setVisible(true);
					EditTaarifView.disposeTable();
					EditTaarifView.disposeExistingButtons();
					EditTaarifView.displayWrnMessage("Prices does not exist!");
					
				}
}
